package Watch;

import java.util.Date;
import java.util.Observable;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;

//class for watching the folder
public class WatchService extends Observable implements Runnable
{
	String folderPath = "";
	volatile boolean pause;
	volatile boolean stop;
	
	/**
	 * Constructor
	 */
	public WatchService()
	{
		pause = false;
		stop = false;
	}
	
	public void setFolderPath(String path)
	{
		folderPath = new String(path);
	}
	
	//function for notifying
	public void notifyAllObj(DirDataChanges changedObj)
	{
		this.setChanged();
		System.out.println("the number is "+this.countObservers());
		this.notifyObservers(changedObj);
	}
	
	class Listener implements JNotifyListener
    {

		@Override
		public void fileCreated(int arg0, String arg1, String arg2) 
		{
			// TODO Auto-generated method stub
			System.out.println("created "+arg0+" "+arg1+" "+arg2);
			//create the object with changes
			DirDataChanges changedObj = new DirDataChanges(arg1+"/"+arg2, "Created");
			//call the method that will notify to all observers about the changes
			notifyAllObj(changedObj);
			
		}

		@Override
		public void fileDeleted(int arg0, String arg1, String arg2) 
		{
			// TODO Auto-generated method stub
			System.out.println("deleted "+arg0);
			DirDataChanges changedObj = new DirDataChanges(arg1+"/"+arg2, "Deleted");
			//call the method that will notify to all observers about the changes
			notifyAllObj(changedObj);
		}

		@Override
		public void fileModified(int arg0, String arg1, String arg2) 
		{
			// TODO Auto-generated method stub
			//System.out.println("modified "+arg1+" "+arg2);
			DirDataChanges changedObj = new DirDataChanges(arg1+"/"+arg2, "Modified", new Date());
			//call the method that will notify to all observers about the changes
			notifyAllObj(changedObj);
		}

		@Override
		public void fileRenamed(int arg0, String arg1, String arg2, String arg3) 
		{
			// TODO Auto-generated method stub
			System.out.println("renamed "+arg2+" "+arg3);
			DirDataChanges changedObj = new DirDataChanges(arg1+"/"+arg2, "Renamed", arg1+"/"+arg3);
			//call the method that will notify to all observers about the changes
			notifyAllObj(changedObj);
		}
		
    }
	

	@Override
	public void run() 
	{
		// TODO Auto-generated method stub
		int mask = JNotify.FILE_CREATED
	       | JNotify.FILE_DELETED
	       | JNotify.FILE_MODIFIED
	       | JNotify.FILE_RENAMED;


		try {
			int watchID = JNotify.addWatch(folderPath, mask, true, new Listener());
		} catch (JNotifyException e) {
		}
	    while(stop == false) 
	    {
	    	allowPause();	// allow this loop to be paused
	    }
	}

	/**
	 * Check the pause flag and insert the thread to the waiting if the pause is true
	 */
	protected void allowPause()
	{
		if(pause)
		{
			System.out.format("paused>\n");
			synchronized (this) 
			{
				try 
				{ 
					wait();
				} catch (InterruptedException e) {}
			}
		}
	}
	
	
	//method for set the pause true
	public void pause(){ pause = true; }
	
	// set the stop true
	public void stop(){ stop = true; }
		
	//change the stop flag to false in order we can continue run
	public void resume() { pause = false; }
}
